package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.ExpandPermissionVO;
import com.cloudeasy.api.vo.user.RolePermissionVO;
import com.cloudeasy.user.po.Permission;
import com.cloudeasy.user.po.Role;
import com.cloudeasy.user.po.RolePermission;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 角色权限工具类
 */
public class RolePermissionUtil {
    /**
     * 转换VO对象
     *
     * @param role            {@link Role}
     * @param rolePermissions {@link List<RolePermission>}
     * @param permissionMap   以Id为key{@link Permission}为value的Map
     * @return {@link RolePermissionVO}
     */
    public static RolePermissionVO convert(Role role, List<RolePermission> rolePermissions, Map<String, Permission> permissionMap) {
        RolePermissionVO rolePermissionVO = new RolePermissionVO();
        rolePermissionVO.setRole(RoleUtil.convert(role));

        rolePermissionVO.setPermissions(rolePermissions.stream().map(rolePermission -> {
            if (rolePermission.getRoleId().equals(role.getId())) {
                ExpandPermissionVO expandPermissionVO = new ExpandPermissionVO();
                BeanUtils.copyProperties(rolePermission, expandPermissionVO);
                BeanUtils.copyProperties(permissionMap.get(rolePermission.getPermissionId()), expandPermissionVO);
                return expandPermissionVO;
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList()));

        return rolePermissionVO;
    }

    /**
     * 转换VO对象
     *
     * @param roles           {@link List<Role>}
     * @param rolePermissions {@link List<RolePermission>}
     * @param permissions     {@link List<Permission>}
     * @return {@link List<RolePermissionVO>}
     */
    public static List<RolePermissionVO> convert(List<Role> roles, List<RolePermission> rolePermissions, List<Permission> permissions) {
        Map<String, Permission> permissionMap = permissions.stream().collect(Collectors.toMap(Permission::getId, permission -> permission, (o, o2) -> o));
        return roles.stream().map(role -> convert(role, rolePermissions, permissionMap)).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param roleIPage       {@link IPage<Role>}
     * @param rolePermissions {@link List<RolePermission>}
     * @param permissions     {@link List<Permission>}
     * @return {@link PageResult<RolePermissionVO>}
     */
    public static PageResult<RolePermissionVO> convert(IPage<Role> roleIPage, List<RolePermission> rolePermissions, List<Permission> permissions) {
        Map<String, Permission> permissionMap = permissions.stream().collect(Collectors.toMap(Permission::getId, permission -> permission, (o, o2) -> o));
        PageResult<RolePermissionVO> rolePermissionVOPageResult = new PageResult<>();
        rolePermissionVOPageResult.setPages(roleIPage.getPages())
                .setTotal(roleIPage.getPages())
                .setRecords(roleIPage.convert(role -> convert(role, rolePermissions, permissionMap)).getRecords());
        return rolePermissionVOPageResult;
    }
}
